package com.artnology.independence.reengineering;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.artnology.independence.Activator;

public class ReengineeringInputDialog extends Dialog {

	private String title;
	private String message;
	private String value = "";
	private Button okButton;
	private Text text;
	private Text errorMessageText;
	private String errorMessage;

	public ReengineeringInputDialog() {
		super(Display.getCurrent().getActiveShell());
		this.title = "Re-engineer for Independence";
		this.message = "Enter prefix or pattern (*, ?) to filter on fully qualified type name";
		value = "*";
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == 2) {
			Activator.getDefault().initializeNewModel();
		}
		if (buttonId == 0 || buttonId == 2) {
			this.value = this.text.getText();
			okPressed();
		} else {
			this.value = null;
			cancelPressed();
		}

	}

	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		if (this.title != null)
			shell.setText(this.title);
	}

	protected void createButtonsForButtonBar(Composite parent) {
		boolean doesGraphExistAlready = Activator.getDefault().getModel()
				.getAllTypes().size() > 0;
		if (doesGraphExistAlready) {
			createButton(parent, 2, "Into New Graph", false);
			okButton = createButton(parent, 0, "Into existing Graph", true);
		} else {
			this.okButton = createButton(parent, 0, IDialogConstants.OK_LABEL,
					true);
		}

		createButton(parent, 1, IDialogConstants.CANCEL_LABEL, false);

		this.text.setFocus();
		if (this.value != null) {
			this.text.setText(this.value);
			this.text.selectAll();
		}
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);

		if (this.message != null) {
			Label label = new Label(composite, 64);
			label.setText(this.message);
			GridData data = new GridData(1796);

			data.widthHint = convertHorizontalDLUsToPixels(300);
			label.setLayoutData(data);
			label.setFont(parent.getFont());
		}
		this.text = new Text(composite, getInputTextStyle());
		this.text.setLayoutData(new GridData(768));
		this.errorMessageText = new Text(composite, 72);
		this.errorMessageText.setLayoutData(new GridData(768));

		this.errorMessageText.setBackground(this.errorMessageText.getDisplay()
				.getSystemColor(22));

		setErrorMessage(this.errorMessage);

		applyDialogFont(composite);
		return composite;
	}

	protected Button getOkButton() {
		return this.okButton;
	}

	protected Text getText() {
		return this.text;
	}

	public String getValue() {
		return this.value;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
		if ((this.errorMessageText != null)
				&& (!this.errorMessageText.isDisposed())) {
			this.errorMessageText.setText(errorMessage == null ? " \n "
					: errorMessage);

			boolean hasError = (errorMessage != null)
					&& (StringConverter.removeWhiteSpaces(errorMessage)
							.length() > 0);
			this.errorMessageText.setEnabled(hasError);
			this.errorMessageText.setVisible(hasError);
			this.errorMessageText.getParent().update();

			Control button = getButton(0);
			if (button != null)
				button.setEnabled(errorMessage == null);
		}
	}

	protected int getInputTextStyle() {
		return 2052;
	}

}
